package com.olympia.myapp.util;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Handler;

public class Reminder {
	ControlListener control;
	TimerTask mTimerTask;
	final Handler handler;
	Timer timer;
	int maxSecond = 60;
	int time = 60;

	public Reminder(ControlListener ctr, int maxS) {
		control = ctr;
		time = maxSecond = maxS;
		timer = new Timer();
		handler = new Handler();
	}

	public void resetTime(int timeNew) {
		time = maxSecond = timeNew;
	}

	public void start(final int seconds) {
		mTimerTask = new TimerTask() {
			public void run() {
				handler.post(new Runnable() {
					public void run() {
						if (time > 0) {
							control.runTask();
							mTimerTask.cancel();
							time--;
							start(seconds);
						} else {
							mTimerTask.cancel();
							control.stopTask();
						}
					}
				});
			}
		};
		// public void schedule (TimerTask task, long delay, long period)
		timer.schedule(mTimerTask, seconds * 1000); //
	}

}